package com.zretc.support.dao;

import com.zretc.support.entity.Topicmult;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.io.Serializable;
import java.util.List;

public interface TopicmultDao extends JpaRepository<Topicmult,String>, JpaSpecificationExecutor<Topicmult>,
        CrudRepository<Topicmult,String>, Serializable {

    @Query(value = "select * from Topicmult where chooseName like %?1% and chooseLevel like %?2% and chooseType like %?3%",nativeQuery = true)
    Page<Topicmult> getTopicmultByPage(String keywords,String level,String type, Pageable pageable);

    @Query(value = "select * from Topicmult where courseId in (?1) and chooseLevel like %?2% and chooseType like %?3%",nativeQuery = true)
    Page<Topicmult> getTopicmultByCourseIdWithPage(List<Long> courseId, String level, String type, Pageable pageable);

    Topicmult findByChooseId(String chooseId);

    /**
     * 随机单选题
     * @param pageable
     * @return
     */
    @Query("SELECT t FROM Topicmult t where t.chooseType=1 and t.course.courseId = ?1 ORDER BY RAND()")
    Page<Topicmult> getRandomChooseSingle(long courseId,Pageable pageable);

    /**
     * 随机多选题
     * @param pageable
     * @return
     */
    @Query("SELECT t FROM Topicmult t where t.chooseType=2 and t.course.courseId = ?1 ORDER BY RAND()")
    Page<Topicmult> getRandomChooseMult(long courseId,Pageable pageable);

    /**
     * 根据id数组查询试题
     * @param chooseId
     * @return
     */
    @Query(value = "select * from Topicmult where chooseId in (?1)",nativeQuery = true)
    List<Topicmult> getByTopicInId(List<String> chooseId);
}
